#!/bin/bash

#Variables
INPUTDIR="../FASTQ/Olig2/NR1"							#complete path to FASTQ files folder
OUTDIR="../output/Experiments/"							#complete path to output directory
REF="../reference/Olig2_NR_Elements.fa"						#complete path to txt file of CRMs indicating where to adapter trim
CONTIG_RANGES="../reference/Trim-Olig2-CRMs.txt"
DELETION_STATUS="True"
PYTHON_SCRIPT="/Users/ajtulloch/MPRA/dMPRA/scripts/plasmid_scripts"		#complete path to plasmid_scripts folder
DESIRED_CONTIG="Olig2_NR1"							#change CRM name


#Specify paths to python scripts
PYTHON_LIBRARY_SCRIPT="$PYTHON_SCRIPT/dMPRA_Plasmid_Library_Analyze_LowMemory.py"
EXPERIMENT_SCRIPT="$PYTHON_SCRIPT/dMPRA_Experiment_Clean.py"

#Load bbduk
export PATH="/Users/ajtulloch/MPRA/bbmap:$PATH" 				#complete path to local bbmap#Generate list of input files
library_set=$(ls $INPUTDIR | grep "R1.fastq.gz" | rev | cut -c 13- | rev)

#Make Outdir
#mkdir $OUTDIR

#Make nested directories for each library
for item in $library_set; do
    mkdir -p "$OUTDIR/$item"
done

#Merge Reads 1 and 2
parallel -j 1 bbmerge.sh in1=$INPUTDIR/{}_R1.fastq.gz in2=$INPUTDIR/{}_R2.fastq.gz out=$OUTDIR/{}/Merged.fastq.gz ::: $library_set

#Align
parallel -j 3 "minimap2 --cs -ax sr $REF $OUTDIR/{}/Merged.fastq.gz | samtools view -bS - | samtools sort -o $OUTDIR/{}/Aligned.bam" ::: $library_set

#Index
parallel -j 3 "samtools index $OUTDIR/{}/Aligned.bam" ::: $library_set

#Run dMPRA Plasmid Library Analysis Python Script
parallel -j 3 "python '$PYTHON_LIBRARY_SCRIPT' {} '$OUTDIR' '$REF' '$CONTIG_RANGES' '$DELETION_STATUS' 'Yes'  '$DESIRED_CONTIG'" ::: $library_set

#Run Read Cleaning Script
parallel -j 1 "python '$EXPERIMENT_SCRIPT' {} '$OUTDIR' '$REF' '$CONTIG_RANGES' '$DELETION_STATUS' '$DESIRED_CONTIG'" ::: $library_set





